package cn.dubito.manager.error;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liliang
 * @description:
 * @date 2018/5/25.
 */
@ControllerAdvice
public class ErrorControllerAdvice {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> handleException(Exception e) {
        Map<String, Object> errorAttributes = new HashMap<>(16);
        String errCode = e.getMessage();
        ErrorEnum byCode = ErrorEnum.getByCode(errCode);
        errorAttributes.put("message", byCode.getMessage());
        errorAttributes.put("code", byCode.getCode());
        errorAttributes.put("canRetry", byCode.isCanRetry());
        errorAttributes.put("type", "advice");
        return new ResponseEntity<>(errorAttributes, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
